package com.souku.finance.application.ui.mine;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.volley.manager.RequestManager;
import com.souku.finance.application.R;
import com.souku.finance.application.base.BaseActivity;
import com.souku.finance.application.bean.InvestRecordDetailsBean;
import com.souku.finance.application.utils.Constant;
import com.souku.finance.application.http.UrlConstact;
import com.souku.finance.application.utils.JsonUtil;
import com.souku.finance.application.utils.MyTextUtil;
import com.souku.finance.application.utils.ParamUtil;
import com.souku.finance.application.utils.ToastUtil;
import com.souku.finance.application.widget.MyTextView;
import com.souku.finance.application.http.volley.ReqListener;
import com.souku.finance.application.http.volley.Success;

import java.util.HashMap;

/**
 * Created by Android on 2017/1/5.
 */

public class InvestRecordDetailsActivity extends BaseActivity implements View.OnClickListener {


    protected RelativeLayout topBack;
    protected TextView topTitle;
    protected TextView tvDealName;
    protected MyTextView tvIncInterestRate;
    protected LinearLayout llAddpoint;
    protected MyTextView tvOriInterestRate;
    protected MyTextView tvIncInterestRate1;
    protected MyTextView tvRatenormal;
    protected MyTextView tvRateadd;
    protected LinearLayout llAddpoint1;
    protected TextView tvBack;
    protected MyTextView tvAmount;
    protected MyTextView tvRealRepayAmount;
    protected Button btnOk;
    private String deailId;
    private InvestRecordDetailsBean bean;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        super.setContentView(R.layout.act_investrecorddetails);
        deailId = getIntent().getStringExtra(Constant.Default);
        initView();
        topTitle.setText("投资详情");
    }

    @Override
    public void internet() {
        getOrderinfo(deailId);
    }

    @Override
    protected void onResume() {
        super.onResume();
        getOrderinfo(deailId);
    }

    private void getOrderinfo(String id) {
        HashMap<String, String> map = ParamUtil.getParams();
        map.put("id", id);
        RequestManager.getInstance().post(UrlConstact.ORDER_INFO, ParamUtil.getSing(map), new ReqListener(this, new Success() {
            @Override
            public void onSuccess(String response) {
                if (JsonUtil.getCode(response) == 1) {
                    bean = JsonUtil.fastBean(JsonUtil.getData(response).toString(), InvestRecordDetailsBean.class);
                    setData();
                } else {
                    ToastUtil.show(JsonUtil.getMessage(response));
                }
            }
        }), ParamUtil.getHeaders());
    }

    @Override
    public void onClick(View view) {
        if (view.getId() == R.id.top_back) {
            finish();
        } else if (view.getId() == R.id.btn_ok) {

        }
    }

    private void initView() {
        topBack = (RelativeLayout) findViewById(R.id.top_back);
        topBack.setOnClickListener(InvestRecordDetailsActivity.this);
        topTitle = (TextView) findViewById(R.id.top_title);
        tvDealName = (TextView) findViewById(R.id.tv_dealName);
        tvIncInterestRate = (MyTextView) findViewById(R.id.tv_incInterestRate);
        llAddpoint = (LinearLayout) findViewById(R.id.ll_addpoint);
        tvOriInterestRate = (MyTextView) findViewById(R.id.tv_oriInterestRate);
        tvIncInterestRate1 = (MyTextView) findViewById(R.id.tv_incInterestRate1);
        tvRatenormal = (MyTextView) findViewById(R.id.tv_ratenormal);
        tvRateadd = (MyTextView) findViewById(R.id.tv_rateadd);
        llAddpoint1 = (LinearLayout) findViewById(R.id.ll_addpoint1);
        tvBack = (TextView) findViewById(R.id.tv_back);
        tvAmount = (MyTextView) findViewById(R.id.tv_amount);
        tvRealRepayAmount = (MyTextView) findViewById(R.id.tv_realRepayAmount);
        btnOk = (Button) findViewById(R.id.btn_ok);
        btnOk.setOnClickListener(InvestRecordDetailsActivity.this);
    }

    private void setData() {
        tvDealName.setText(bean.getDealName());
        if(Double.parseDouble(bean.getIncInterestRate())==0){
            llAddpoint.setVisibility(View.GONE);
            llAddpoint1.setVisibility(View.GONE);
            tvRatenormal.setText(MyTextUtil.setMoneyToElement(bean.getInterest()));
        }else {
            llAddpoint.setVisibility(View.VISIBLE);
            llAddpoint1.setVisibility(View.VISIBLE);
            tvIncInterestRate.setText(bean.getIncInterestRate());
            tvIncInterestRate1.setText(bean.getIncInterestRate());
            double rateAdd=MyTextUtil.calculationMoney(Integer.parseInt(bean.getBenifitDays()),Double.parseDouble(bean.getIncInterestRate())/100,Integer.parseInt(bean.getAmount()));
            tvRateadd.setText(MyTextUtil.setMoneyToElement(rateAdd+""));
            tvRatenormal.setText(MyTextUtil.setMoneyToElement(Integer.parseInt(bean.getInterest())-rateAdd+""));
        }
        if(bean.getHasRepayed().equals("1")){
            tvBack.setText("已回款");
        }else {
            tvBack.setText("未回款");
        }
        tvOriInterestRate.setText(MyTextUtil.doubleFormat(Double.parseDouble(bean.getInterestRate())-Double.parseDouble(bean.getIncInterestRate())));
        tvAmount.setText(MyTextUtil.setMoneyToElement(bean.getAmount()));
        tvRealRepayAmount.setText(MyTextUtil.setMoneyToElement(bean.getRealRepayAmount()));
        btnOk.setEnabled(true);
    }
}
